from commons.entity.po.travel import Travel
from commons.entity.po.user import User

from commons.entity.po.model import db


class TravelApproval(db.Model):
    __tablename__ = 'travel_approval'

    travel_approval_id = db.Column(
        db.Integer, primary_key=True, autoincrement=True, info='经理审批出差申请记录编号')
    travel_id = db.Column(db.ForeignKey(
        'travel.travel_id'), index=True, info='出差编号')
    manager_id = db.Column(db.ForeignKey(
        'user.user_id'), index=True, info='经理编号')
    date = db.Column(db.Date, info='审批日期')

    manager = db.relationship('User', primaryjoin='TravelApproval.manager_id == User.user_id',
                              backref='travel_approvals')
    travel = db.relationship('Travel', primaryjoin='TravelApproval.travel_id == Travel.travel_id',
                             backref='travel_approvals')

    def to_dict(self):
        return {
            "travel_approval_id": self.travel_approval_id,
            "travel_id": self.travel_id,
            "manager_id": self.manager_id,
            "date": self.date.isoformat() if self.date else None
        }

    def to_dict_2(self):
        return {
            "travelApprovalId": self.travel_approval_id,
            "travelId": self.travel_id,
            "managerId": self.manager_id,
            "date": self.date.isoformat() if self.date else None
        }
